package com.ruoyi.web.controller.community;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.community.domain.*;
import com.ruoyi.community.service.message.MessageService;
import com.ruoyi.community.utils.MessageUtil;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/community/message")
public class MessageController {

    @Autowired
    MessageService messageService;

    @PostMapping("/announce")
    public AjaxResult test(@RequestBody Announce announce) {
        announce.setType(AnnounceType.TONGYONG);
        return AjaxResult.success(MessageUtil.addAnnounce(announce));
    }

    @PostMapping("/announceList2")
    public AjaxResult test4(@RequestBody Announce announce) {
        announce.setType(AnnounceType.TONGYONG);
        List<Long> ids = new ArrayList<>();
        ids.add(16L);
        ids.add(17L);
        return AjaxResult.success(MessageUtil.addAnnounceList(announce, ids));
    }

    @PostMapping("/remind")
    public AjaxResult test2(@RequestBody Remind remind) {
        remind.setSenderAction(RemindAction.DIANZAN);
        return AjaxResult.success(MessageUtil.addRemind(remind));
    }

    @PostMapping("/remindList")
    public AjaxResult test3(@RequestBody List<Remind> list) {

        for(Remind remind : list) {
            remind.setSenderAction(RemindAction.DIANZAN);
        }
        return AjaxResult.success(MessageUtil.addRemindList(list));
    }

    /**
     * 获取未读消息个数
     */
    @GetMapping("/getMessageCount")
    public AjaxResult getMessageCount() {
        int announceCount = messageService.getUnreadAnnounceCount();
        int remindCount = messageService.getUnreadRemindCount();
        Map<String, Integer> map = new HashMap<>();
        map.put("announce", announceCount);
        map.put("remind", remindCount);
        return AjaxResult.success(map);
    }

    /**
     * 获取未读公告个数
     */
    @GetMapping("/getUnreadAnnounceCount")
    public AjaxResult getUnreadAnnounceCount() {
        int announceCount = messageService.getUnreadAnnounceCount();
        return AjaxResult.success(announceCount);
    }

    /**
     * 分页获取公告列表，按公告发送的时间排序
     */
    @GetMapping("/announceList")
    public AjaxResult getAnnounceList(@RequestParam Integer page) {
        List<MessageAnnounceUser> announceList = messageService.getAnnounceList(page);
        return AjaxResult.success(announceList);
    }

    /**
     * 根据id查询公告内容，并更新阅读状态
     */
    @PostMapping("/getAnnounce")
    public AjaxResult getAnnounce(@RequestBody MessageAnnounceUser messageAnnounceUser) {
        System.out.println(messageAnnounceUser);
        MessageAnnounce announce = messageService.getAnnounceAndUpdateStatus(messageAnnounceUser);
        return AjaxResult.success(announce);
    }

    /**
     * 获取未读提醒个数
     */
    @GetMapping("/getUnreadRemindCount")
    public AjaxResult getUnreadRemindCount() {
        int remindCount = messageService.getUnreadRemindCount();
        return AjaxResult.success(remindCount);
    }

    /**
     * 分页获取提醒列表，按提醒发送的时间排序
     */
    @GetMapping("/remindList")
    public AjaxResult getRemindList(@RequestParam Integer page) {
        System.out.println(page);
        List<MessageRemind> remindList = messageService.getRemindList(page);
        return AjaxResult.success(remindList);
    }

    /**
     * 根据id查询提醒内容，并更新阅读状态
     */
    @PostMapping("/getRemind")
    public AjaxResult getRemind(@RequestBody MessageRemind messageRemind) {
        System.out.println(messageRemind);
        MessageRemind remind = messageService.getRemindAndUpdateRemind(messageRemind);
        return AjaxResult.success(remind);
    }
}
